using System;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sedna.Core.Controller;
using Sedna.Core.Controller.Configurations;
using Sedna.Core.Controls.Support.Javascript;
//using DataBinder = Sedna.Core.DataBinding.DataBinder;

namespace Sedna.Core.Controls.Support
{
	/// <summary>
	/// Summary description for BaseCondition.
	/// </summary>
	public class BaseCondition: BaseControl, IClientScriptingControl, INamingContainer
	{
		private DropDownList dropDown;
        private WebControl blinker;
        private object invisibleValue;

	    private WebControl Blinker
	    {
	        get
	        {
	            EnsureChildControls();
	            return blinker;
	        }
	    }


	    public DropDownList DropDown
	    {
	        get
	        {
                EnsureChildControls();
                return dropDown;
	        }
	    }
	    
	    public string Condition
	    {
            get { return DropDown.SelectedValue;  }
            set { DropDown.SelectedValue = value; }
	    }

	    public new IConditionConfiguration Configuration
	    {
            get { return (IConditionConfiguration) base.Configuration; }
            set { base.Configuration = value; }
	    }
	    
		public override bool ClientScriptingEnabled
		{
			get { return true; }
			set { /* always enabled */}
		}

		protected object Value
		{
            get { return ((IValueProvider) blinker).Value; }
            set { ((IValueProvider) blinker).Value = value; }
		}

		/// <summary>
		/// Value of DropDown, that causes Blinker to disappear.
		/// </summary>
		public object InvisibleValue
		{
			get { return invisibleValue; }
			set { invisibleValue = value; }
		}


		public override bool Enabled
		{
			get { return base.Enabled; }
			set
			{
				EnsureChildControls();
				base.Enabled = value;
				Blinker.Enabled = value;
				DropDown.Enabled=value;
			}
		}


		protected override HtmlTextWriterTag TagKey
		{
			get { return HtmlTextWriterTag.Table; }
		}

		public BaseCondition():base()
		{
			Initialize();
		}

		public BaseCondition(IConfiguration configuration) : base(configuration)
		{
			Initialize();
		}


		private void Initialize()
		{
			Width=Unit.Pixel(200);	
		}

		protected override void Build()
		{
			base.Build();
			if (!Configuration.Width.IsEmpty)
			{
				Width=Configuration.Width;
			}
            InvisibleValue=Configuration.InvisibleValue;
			ClientVariable=Configuration.ClientVariable;
			ClientScriptingEnabled=Configuration.ClientScriptingEnabled;
		}

		protected override void CreateChildControls() 
		{
		    dropDown = BuildDropDown();
            blinker = BuildBlinker();

            blinker.Style.Remove("visibility");
            blinker.Style.Add("visibility", "hidden");
		    
		    TableRow row = new TableRow();
			Controls.Add(row);

			TableCell dropDownCell=new TableCell();
			dropDownCell.Width=Unit.Percentage(0);
			row.Controls.Add(dropDownCell);

			TableCell blinkingCell=new TableCell();
			blinkingCell.Width=Unit.Percentage(100);
			blinkingCell.Style.Add("padding-left", "3px");
			row.Controls.Add(blinkingCell);

			WebControl control=Blinker;
			control.Width=Unit.Percentage(100);
			DropDown.Width=Unit.Pixel(80);
			dropDownCell.Controls.Add(DropDown);
			blinkingCell.Controls.Add(control);

		}

        protected virtual WebControl BuildBlinker()
        {
            return (WebControl)ControlBuilder.BuildControl(Configuration.BlinkerConfiguration);
        }

        protected virtual DropDownList BuildDropDown()
	    {
	        return (DropDownList)ControlBuilder.BuildControl(Configuration.DropDownListConfiguration);
	    }

	    protected override void AddAttributesToRender(HtmlTextWriter writer)
		{
			base.AddAttributesToRender(writer);
			writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "0");
			writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0");
		}

		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);
			if (ClientScriptingEnabled)
			{
			    DeclareScriptVariable();
			}
		}


		protected override void Unbind()
		{
			binder.Unbind(new string[]{"Enabled", "Condition"});
            binder.Unbind("Value", Configuration.DataParser);
		}

		protected override void Bind()
		{
			binder.Bind(new string[]{"Enabled", "Condition"});
			Enabled=Enabled&&configuration.IsEnabledForPrincipal();
        }

        #region Javascript support
        protected override void DeclareScriptVariable()
        {
            DropDown.ClientScriptingEnabled = true;
            ((IClientScriptingControl)Blinker).ClientScriptingEnabled = true;
            base.DeclareScriptVariable();
        }

	    protected override IDictionary BuildScriptParameters()
	    {
	        IDictionary parameters =  base.BuildScriptParameters();
            parameters["dropDown"] = DropDown.ClientVariable;
            parameters["blinker"] = ((IClientScriptingControl)Blinker).ClientVariable;
            parameters["invisibleValue"] = InvisibleValue;
	        parameters["submitInvisible"] =
	            new ScriptObjectGenerator(
	                Object.Equals(DropDown.SelectedValue, InvisibleValue != null ? InvisibleValue.ToString() : String.Empty)).ToScript();
            return parameters;
	    }

	    #endregion

    }
}
